<template>
<div class="main_box">
	<div class="head_box" :style="{backgroundColor:skin}">
		<div class="logo_box">
			<a @click="goto_page('manage_home','home_index')">
				<img :src="cfg.sys_logo" v-if="cfg.sys_logo!=''">
				<img src="/../../assets/images/logo.png" v-else>
			</a>
		</div>
		<div class="menu_box">
			<ul>
				<li v-for="(val,index) in menulist"  :class="{cur:active_name==val.name}"><a @click="sel_menu(val.name)">{{val.label}}</a></li>
			</ul>
		</div>
		<div class="right_menu">
			<ul>
				<li>
					<a @click="logout" title="退出登录"><Icon type="md-power" /></a>
				</li>
				<li>
					<a title="更换皮肤"><Icon type="md-color-palette" /></a>
					<div class="pop_box skin_box">
						<div class="skin_list">
							<div class="item_box" v-for="val in skins">
								<a class="color" :style="{backgroundColor:val.color}" @click="setSkin(val.color)">
									<span>点击换肤</span>
								</a>
							</div>
						</div>
					</div>
				</li>
				<li>
					<a title="系统消息">
						<Badge :count="message.count" style="display: inline;"><Icon type="md-notifications" /></Badge>
					</a>
					<div class="pop_box notify_box">
						<div class="box">
							<div class="box_ti">
								<h3>消息提示</h3>
							</div>
							<div class="box_con">
								<ul style="line-height: 30px;">
									<li v-for="val in message.list">
										<a style="color: #333;" @click="$router.push({name:'n_system'})">
											<span style="float:right">(<em style="color:red;font-style: normal;">{{val.msg_num}}</em>)</span>
											<span>{{val.name}}</span>
										</a>
									</li>
								</ul>
								<a style="display: block;text-align: center;" @click="$router.push({name:'n_system'})">更多消息></a>
							</div>
						</div>
						<div class="box" v-if="crrencys">
							<div class="box_ti">
								<h3>当前汇率</h3>
								<span style="float:right;color:red;">{{crrencys.date}}</span>
							</div>
							<div class="box_con">
								<ul style="line-height: 30px;">
									<li v-for="val in crrencys.list">
										<span style="float:right;color:rgb(26, 188, 156);">{{val.mark}}{{val.exchange_rate}}</span>
										<span style="">{{val.name}}</span>
									</li>
								</ul>
								<a style="display: block;text-align: center;" @click="$router.push({name:'s_multicurrency'})">汇率设置></a>
							</div>
						</div>
					</div>
				</li>
				<li>
					<a title="常用菜单"><Icon type="md-menu" /></a>
					<div class="pop_box pmenu_box">
						<div class="box">
							<div class="box_ti">
								<h3>常用菜单</h3>
								<a style="float:right;" @click="menu_show=!menu_show">菜单管理</a>
							</div>
							<div class="box_con">
								<ul >
									<li v-for="val in common_menus" style="width:33%;float:left;text-align:center;line-height: 45px;">
										<a @click="sel_subMenu(val.name)">{{val.label}}</a>
									</li>
								</ul>
							</div>
						</div>
					</div>
				</li>
				<li>
					<a title="系统首页" @click="goto_page('manage_home','home_index')"><Icon type="md-home" /></a>
				</li>
				<li>
					<span style="color: #fff;margin-right: 12px;">
						<Avatar :src="userAvator"/>
						{{user.nickname}}
					</span>
					<div class="pop_box puser_box" style="color: #666;">
						<div class="box">
							<div class="box_ti">
								<h3>账户信息</h3>
								<a style="float:right;" @click="goto_page('manage_home','my_account')">账户设置</a>
							</div>
							<div class="box_con">
								<ul>
									<li style="display:block;">
										<span style="color: #aaa;margin-right: 10px;">.所在部门</span>
										{{user.bumen}}
									</li>
									<li style="display:block;">
										<span style="color: #aaa;margin-right: 10px;">.本次登录</span>
										{{user.last_login}}
									</li>
									<li style="display:block;">
										<span style="color: #aaa;margin-right: 10px;">.登录地区</span>
										{{user.login_region}}({{user.login_ip}})
									</li>
									<li style="display:block;">
										<span style="color: #aaa;margin-right: 10px;">.上次登录</span>
										{{user.prev_time}}
									</li>
								</ul>
							</div>
						</div>
					</div>
				</li>
			</ul>
		</div>
	</div>
	<div class="content_box">
		<div class="left_box">
			<div class="left_con">
				<dl v-for="menu in subMenu" class="mmenu_box">
					<dt>{{menu.label}}</dt>
					<dd v-for="(subitem,index) in menu.children" :class="{cur:subMenu_sel==subitem.name}">
						<a @click="sel_subMenu(subitem.name)">{{subitem.label}}</a>
					</dd>
				</dl>
			</div>
		</div>
		<div class="right_box">
			<div class="mlayer_box">
				
				<div style="min-height: 800px;">
					<router-view class="mlayer_con" @change="change" @update_menu="update_menu" v-if="isRouterAlive" />
				</div>
				<div class="footer" style="margin: 30px 0;">
					<p style="color: #ccc;text-align: center;">Copyright © www.alphaleader.com, All Rights Reserved</p>
				</div>
			</div>
		</div>
	</div>
	<menubox v-model="menu_show" :data="menulist" @success="menu_success" :menus="common_menus"></menubox>
</div>
</template>

<script>
import userAvator from './../assets/images/users.png';
import Wcore from '@/libs/common';
import menubox from './menu_box';

export default {
	name:'home',
	provide(){
		return {
			reload:this.reload
		};
	},
    data () {
        return {
			menu_show:false,
			isRouterAlive:true,
			default_menu:[
				{name:'manage_home',label:'首页',priv:'no_priv',children:[
					{name:'m_home',label:'系统首页',priv:'no_priv',children:[
						{name:'home_index',label:'系统首页',priv:'no_priv'},
						{name:'my_account',label:'账户设置',priv:'no_priv'}, 
						{name:'login_log',label:'登录日志',priv:'no_priv'}
					]},
				]},
				{name:'manage_chuna',label:'出纳',priv:'chuna',children:[
					{name:'m_cash',label:'出纳管理',priv:'chuna',children:[
						{name:'c_cash',label:'现金日记账',priv:'chuna_cash'},
						{name:'c_bank',label:'银行日记账',priv:'chuna_bank'},
						{name:'c_income',label:'业务收入表',priv:'chuna_yewu'},
						{name:'c_jiezhi',label:'借支表',priv:'chuna_jiezhi'},
					]},
					{name:'c_balance',label:'出纳结账',priv:'chuna',children:[
						{name:'c_balance_last',label:'出纳期末结账',priv:'chuna_balance'},
						{name:'c_balance_fan',label:'出纳期反结账',priv:'chuna_fan'},
					]},
					
					
				]},
				{name:'mamage_accounting',label:'会计',priv:'accounting',children:[
					{name:'m_accoun',label:'会计管理',priv:'accounting',children:[
						{name:'a_accounts',label:'对账处理',priv:'accounting_duizhang'},
						{name:'a_fixed_assets',label:'固定资产/长期摊销',priv:'accounting_assets'},
						{name:'a_share_member',label:'人员分摊表',priv:'accounting_member'},
						{name:'a_share_income',label:'收入分摊表',priv:'accounting_income'},
					]},
					{name:'a_balance',label:'会计结账',priv:'accounting',children:[
						{name:'a_balance_last',label:'会计期末结账',priv:'accounting_balance'},
						{name:'a_balance_fan',label:'会计期反结账',priv:'accounting_fan'},
					]},
					
				]},
				{name:'mamage_report',label:'报表',priv:'report',children:[
					{name:'m_report',label:'报表管理',priv:'report',children:[
						{name:'r_sunyi_fengzi',label:'风资损益表',priv:'report_fengzi'},
						{name:'r_sunyi_team',label:'团队损益表',priv:'report_team'},
						//{name:'borrow_support',label:'借支记录表'},
					]}
				]},
				/*
				{name:'manage_baoxiao',label:'报销',children:[
					{name:'m_baoxiao',label:'报销管理',path:'',children:[
						//暂不用开发
					]}
				]},
				{name:'manage_yusuan',label:'预算',children:[
					{name:'m_yusuan',label:'预算管理',path:'',children:[
						//暂不用开发
					]}
				]},
				*/
				{name:'manage_notice',label:'通知',priv:'notice',children:[
					{name:'m_notice',label:'消息查询',priv:'notice',children:[
						{name:'n_system',label:'系统消息',priv:'notice_system'},
						//{name:'n_station',label:'站内信消息',priv:'notice_station'},
						//{name:'n_gonggao',label:'公告消息',priv:'notice_gonggao'},
						{name:'n_template',label:'消息模板',priv:'notice_template'},
					]}
				]},
				{name:'manage_setting',label:'设置',priv:'setting',children:[
					{name:'m_system',label:'系统设置',priv:'setting_system',children:[
						{name:'s_system',label:'系统信息',priv:'setting_system'},
					]},
					{name:'m_user',label:'用户管理',priv:'setting_user',children:[
						{name:'s_user',label:'用户列表',priv:'setting_user'},
					]},
					{name:'m_multicurrency',label:'币种管理',priv:'setting_multicurrency',children:[
						{name:'s_multicurrency',label:'币种列表',priv:'setting_multicurrency'},
					]},
					{name:'m_subject',label:'科目管理',priv:'setting_subject',children:[
						{name:'s_subject',label:'科目列表',priv:'setting_subject'},
					]},
					{name:'m_company',label:'公司主体',priv:'setting_company',children:[
						{name:'s_company',label:'公司主体列表',priv:'setting_company'},
						{name:'s_bumen',label:'部门列表',priv:'setting_bumen'},
						{name:'s_area',label:'区域管理',priv:'setting_area'},
					]},
				]},
				{name:'manage_role',label:'权限',priv:'role',children:[
					{name:'m_role',label:'权限管理',priv:'role',children:[
						{name:'r_role',label:'权限组管理',priv:'role'},
						{name:'r_log',label:'操作日志',priv:'role'},
					]},
				]},
				
			],
            menulist:[],
			subMenu:[],
			active_name:'manage_home',
			openNames:[],
			userAvator:userAvator,
			subMenu_sel:'home',
			skin:'#666',
			skins:[],
			common_menus:[],
			crrencys:[],
			message:{
				count:0,
				list:[]
			},
			cfg:{
				sys_logo:''
			},
			router_name:''
        };
    },
	components:{
		menubox
	},
	created(){
		
	},
	mounted(){
		
		this.menulist = this.get_menu();
		
		this.load_data();

		if(this.menulist.length>0){
			this.subMenu = this.menulist[0].children;
		}
		
		
		this.skins.push({color:'#666666'});
		this.skins.push({color:'#3498db'});
		this.skins.push({color:'#ed6e4d'});
		this.skins.push({color:'#fabb3d'});
		this.skins.push({color:'#aa7ab3'});
		this.skins.push({color:'#9ad0b9'});
		this.skins.push({color:'#ea94be'});
		this.skins.push({color:'#c1374a'});
		
		
		var color = Wcore.getData('skin');
		if(color){
			this.skin = color;
		}
		
		this.load_config();
		this.init_menu();
		this.load_currencys();
		this.load_msg_type();
		this.load_common_menus();
	},
	computed:{
		user(){
			
			return this.$store.state.user.user_info;
		}
	},
	watch:{
		
	},
    methods: {
		load_config(){
			var self = this;
			var postdata = {
				
			};
			Whttp.load('Admin/Login/get_config',postdata,function(result){
				self.cfg.sys_logo = result['sys_logo'];
			});
		},
		init_menu(){
			var name = this.$route.name;
			var top_name = '';
			var is_break = false;
			for(var i=0;i<this.menulist.length;i++){
				var ls_child = this.menulist[i].children;
				
				for(var j=0;j<ls_child.length;j++){
					var ls_sub = ls_child[j].children;
					
					for(var k=0;k<ls_sub.length;k++){
						if(name==ls_sub[k].name){
							top_name = this.menulist[i].name;
							is_break = true;
							break;
						}
					}
					if(is_break){
						break;
					}
				}
				if(is_break){
					break;
				}
			}
			
			if(top_name!=''){
				this.goto_page(top_name,name);
			}
		},
		get_menu(){
			try{
				if(this.user){
					var privs = this.user.privs.split(',');
				}
				else{
					var privs = [];
				}
			}
			catch(e){
				window.location.reload();
			}
			
			var list = this._getData(this.default_menu);
			
			var arr = [];
			for(var i=0;i<list.length;i++){
				var priv = list[i].name;
				if(list[i].priv){
					priv = list[i].priv;
				}
				
				
				if(this._checkPriv(priv,privs)){
					var childrens = list[i].children;
					
					var ls = get_attr(list[i]);
					ls.children = [];
					
					for(var j=0;j<childrens.length;j++){
						var childs = childrens[j].children;
						
						var ls_item = get_attr(childrens[j]);
						ls_item.children = [];
						
						
						for(var k=0;k<childs.length;k++){
							var ls_child = get_attr(childs[k]);
							var ls_priv1 = ls_child.name;
							if(ls_child.priv){
								ls_priv1 = ls_child.priv;
							}
							
							if(this._checkPriv(ls_priv1,privs)){
								ls_item.children.push(ls_child);
							}
						}
						
						var ls_priv = ls_item.name;
						if(ls_item.priv){
							ls_priv = ls_item.priv;
						}
						
						if(this._checkPriv(ls_priv,privs)){
							ls.children.push(ls_item);
						}
					}
					arr.push(ls);
				}
			}	
			
			function get_attr(data){
				var ls_arr = {};
				for(var i in data){
					if(i!='children'){
						ls_arr[i] = data[i];
					}
				}
				return ls_arr;
			}
			return arr;
		},
		load_data(){
			
			var self = this;
			Whttp.load('System/Module/get_menu_list',{},function(result){
				
				var menus = self.menulist;
	
				for(var j=0;j<result.length;j++){
					var index = self.has_menu(result[j].name,menus);
					if(index===false){
						self.add_menu(result[j],menus);
					}
					else{
						if(result[j].children){
							for(var k=0;k<result[j].children.length;k++){
								var ind = self.has_menu(result[j].children[k].name,menus[index].children);
								if(ind===false){
									self.add_menu(result[j].children[k],menus[index].children);
								}
							}
						}
						
					}
				}
			});
		},
		sel_menu(name){
		
			this.active_name = name;
			for(var i=0;i<this.menulist.length;i++){
				if(this.menulist[i].name==name){
					this.subMenu = this.menulist[i].children;
					break;
				}
			}
			
		},
		change(name){
			//this.active_name = name;
			
		},
		has_menu(name,arr){
			
			for(var i=0;i<arr.length;i++){
				if(arr[i].name==name){
					return i;
				}
			}
			return false;
		},
		add_menu(data,arr){
		
			var menu = {
				name:data.name,
				label:data.label,
				children:[]
			};
			
			if(Array.isArray(data.children)){
				var children = [];
				for(var i=0;i<data.children.length;i++){
					
					this.add_menu(data.children[i],children);
					
				}
				menu.children = children;
			}
			
			if(data.router){
				var router = data.router;
				
				for(var i=0;i<router.length;i++){
					//router[i].component = this.get_component(router[i].component);
				}
				
				
				//this.$router.addRoutes(router) //动态添加路由
			}
			
			arr.push(menu);
		},
		get_component(uri){
		
			
			return;
			var promises = new Promise(function(resolve){
				
				Whttp.load('System/Module/get_component',{path:uri},function(result){
					var aa = eval('('+result+')');
					resolve(aa);
				})
			});
			
			return promises;
		},
		update_menu(){
			this.menulist = this._getData(this.default_menu);
			this.load_data();
		},
		sel_subMenu(name){
			this.subMenu_sel = name;
			if (this.router_name == name || localStorage.getItem('router_name') == name ){
				return ;
			}
			this.router_name = name;
			localStorage.setItem('router_name',name);
			this.$router.push({name:name});
		},
		goto_page(menu_name,sub_name){
			this.sel_menu(menu_name);
			this.sel_subMenu(sub_name);
			
		},
		setSkin(color){
			this.skin = color;
			Wcore.setData('skin',color);
		},
		logout(){
		
			Wcore.setData(Wcore.prefix('token'),'');
			this.$router.push({name:'login'});
			this.$Message.info('退出成功');
		},
		reload(){
			this.isRouterAlive = false;
			this.$nextTick(function(){
				this.isRouterAlive = true;
			});
		},
		load_currencys(){
			var self = this;
			var postdata = {
				
			};
			//Whttp.load('User/Currency/get_new',postdata,function(result){console.log(result);
			Whttp.load('User/Currency/get_fixed_coin',postdata,function(result){
				self.crrencys = result;
			});
		},
		load_msg_type(){
			var self = this;
			var postdata = {
				
			};
			
			
			Whttp.load('System/Message/get_new',postdata,function(result){
				self.message.count = parseInt(result.count);
				self.message.list = result.list;
			});
		},
		load_common_menus(){
			var self = this;
			var postdata = {
				
			};
			
			var mdata = [];
			var list = this.menulist;
			for(var i=0;i<list.length;i++){
			
				var children = [];
				var ls_children = list[i].children;
				
				for(var j=0;j<ls_children.length;j++){
					var ls_child = ls_children[j].children;
					if(ls_child){
						for(var k=0;k<ls_child.length;k++){
						
							children.push({
								name:ls_child[k].name,
								label:ls_child[k].label,
							});
						}
					}
					else{
						children.push({
							name:ls_children[j].name,
							label:ls_children[j].label,
						});
					}
				}
				
				var ls_data = {
					name:list[i].name,
					label:list[i].label,
					children:children,
				};
				mdata.push(ls_data);
			}
			
			Whttp.load('System/Menus/get_menu',postdata,function(result){
				self.common_menus = [];
				for(var i=0;i<mdata.length;i++){
					var child = mdata[i].children;
					for(var j=0;j<child.length;j++){
						if(result.in_array(child[j].name)){
							self.common_menus.push(child[j]);
						}
					}
				}
				
			});
		},
		menu_success(result){
			this.load_common_menus();
		}
    }
};
</script>

<style scoped >
    @import './index.css';
</style>